package com.zmn.oms.admin.controller.work.modify;

import com.zmn.base.common.data.common.dro.tags.TagsDRO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.business.interfaces.remind.OrderRemindBService;
import com.zmn.oms.common.constant.OrderBaseCodeMapConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.dto.remind.RemindDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName WorkReminderController
 * @description: 手动催单
 * @author: wangyanci
 * @create: 2021-07-12 15:33
 * @Version 1.0
 **/
@Slf4j
@Controller
@RequestMapping("/work/modify")
public class WorkReminderController extends WorkBaseController {

    @Autowired
    private OrderRemindBService orderRemindBService;
    @Autowired
    ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;

    @RequestMapping(value = "/reminder")
    public ModelAndView reminder(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/orderRemind");
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        // 公共配置获取催单场景id
        List<TagsDRO> remindTagsDROS = baseCodeService.listBaseCodeMap(GlobalConsts.PLAT_MARK_ZMN, OrderBaseCodeMapConsts.CODE_MAP_ONE_REMIND_SCENES, orderWork.getType(), null);
        if (CollectionUtil.isNotNullOrEmpty(remindTagsDROS)) {
            // name为缘由名称 二级缘由为单据类型
            List<KVDict> collect = remindTagsDROS.stream().map(dro -> KVDict.builder().value(dro.getThreeTypeId())
                    .text(dro.getName()).ext1(dro.getDesc())
                    .selected(StringUtil.isNotBlank(dro.getDesc())).build())
                    .collect(Collectors.toList());
            mv.addObject("remindScenes", collect);
        }
        // 公共配置获取联系场景id
        List<TagsDRO> contactTagsDROS = baseCodeService.listBaseCodeMap(GlobalConsts.PLAT_MARK_ZMN, OrderBaseCodeMapConsts.CODE_MAP_ONE_CONTACT_SCENES, null, null);
        if (CollectionUtil.isNotNullOrEmpty(contactTagsDROS)) {
            List<KVDict> collect = contactTagsDROS.stream().map(dro -> KVDict.builder().value(dro.getTwoTypeId())
                    .text(dro.getName()).ext1(dro.getDesc())
                    .selected(Objects.nonNull(dro.getTwoTypeId()))
                    .build())
                    .collect(Collectors.toList());
            mv.addObject("contactScenes", collect);
        }
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        return mv;
    }

    @RequestMapping(value = "/save/reminder")
    @ResponseBody
    public ResultDTO saveRemind(@RequestBody RemindDTO remindDTO) {
        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.oneClickReminderCheck(remindDTO);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        try {
            if (Objects.equals(remindDTO.getType(), GlobalConsts.YES)) {
                // 师傅联系用户
                orderRemindBService.remindMasterContactUser(remindDTO);
            } else {
                // 联系师傅
                orderRemindBService.remindContactMaster(remindDTO);
            }
            return ResultDTO.success();
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }
}